﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WMSSoft.Lib.WinKStart
{

    public static class cLanguageLoader
    {
        private static Dictionary<string, List<string>> m_AvailableLanguages = new Dictionary<string, List<string>>();

        public static void Load()
        {
            try
            {
                m_AvailableLanguages.Clear(); 

                if (System.IO.Directory.Exists(cUserSettingsReader.LanguagesPath) == true)
                {
                    string[] tmpLanguageFiles = System.IO.Directory.GetFiles(cUserSettingsReader.LanguagesPath);
                    foreach (string Item in tmpLanguageFiles)
                    {
                        LoadLanguageFile(Item);
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Can't load Languages!", Ex);
            }
        }

        public static void SetLanguage(string LanguageKey)
        {
            try
            {
                if (m_AvailableLanguages.ContainsKey(LanguageKey) == true)
                {
                    List<string> tmpLanguageFile = m_AvailableLanguages[LanguageKey];
                    foreach (string Item in tmpLanguageFile)
                    {
                        LoadLanguageItems(Item);
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Can't set language!", Ex);
            }
        }

        #region Private Methods
        private static void LoadLanguageFile(string Filename)
        {
            try
            {
                if (System.IO.File.Exists(Filename) == true)
                {
                    System.Xml.XmlDocument tmpDocument = new System.Xml.XmlDocument();
                    tmpDocument.Load(Filename);

                    System.Xml.XmlNode tmpRootNode = Basic.Xml.cXmlHelpers.GetRootNode(tmpDocument);
                    //load language key from rootnode attribute
                    string sLanguageKey = tmpRootNode.Attributes["Language"].Value;

                    if (m_AvailableLanguages.ContainsKey(sLanguageKey) == false)
                    {
                        m_AvailableLanguages.Add(sLanguageKey,  new List<string>());
                        m_AvailableLanguages[sLanguageKey].Add(Filename);
                    }
                    else
                    {
                        //Language already exist
                        //add language items from file to existing language

                        if (m_AvailableLanguages[sLanguageKey].Contains(Filename) == false) 
                            m_AvailableLanguages[sLanguageKey].Add(Filename);
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Can't load languageFile!", Ex);
            }
        }

        private static void LoadLanguageItems(string Filename)
        {
            try
            {
                if (System.IO.File.Exists(Filename) == true)
                {
                    System.Xml.XmlDocument tmpDocument = new System.Xml.XmlDocument();
                    tmpDocument.Load(Filename);

                    System.Xml.XmlNode tmpRootNode = Basic.Xml.cXmlHelpers.GetRootNode(tmpDocument);
                    System.Xml.XmlNode tmpHeadNode = WMSSoft.Lib.Basic.Xml.cXmlHelpers.GetElementNode(tmpRootNode, "strings");
                    if (tmpHeadNode != null)
                    {
                        Basic.LanguageStrings.LoadStrings(tmpHeadNode);
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception("Can't load Strings from LanguageFile!", Ex);
            }
        }
        #endregion
    }
}
